package com.marvels.common.data;

import java.util.HashMap;

/**
 * 
* @ClassName: DataRow 
* @Description:封装hashmap，方便开发，不用写代码转型  
* @author 李瑶
* @date 2018年1月16日 上午10:15:24 
*
 */
public class DataRow extends HashMap{

    @SuppressWarnings("unchecked")
    
	public void set(String name, String value) {
        if (name == null || name.equals("")){
        	return;
        }
        if (value == null){
        	put(name, "");
        }else{
        	put(name, value);
        }
    }

    @SuppressWarnings("unchecked")
	public void set(String name, int value) {
        put(name, Integer.valueOf(value));
    }

    @SuppressWarnings("unchecked")
	public void set(String name, boolean value) {
        put(name, Boolean.valueOf(value));
    }

    @SuppressWarnings("unchecked")
	public void set(String name, long value) {
        put(name, Long.valueOf(value));
    }

    @SuppressWarnings("unchecked")
	public void set(String name, float value) {
        put(name, Float.valueOf(value));
    }

    @SuppressWarnings("unchecked")
	public void set(String name, double value) {
        put(name, Double.valueOf(value));
    }

    @SuppressWarnings("unchecked")
	public void set(String name, Object value) {
        put(name, value);
    }

    public String getString(String name) {
        if (name == null || name.equals("")){
        	return "";
        }
        Object obj = this.get(name);
        return (obj == null) ? "" : obj.toString();
    }

    public int getInt(String name) {
        if (name == null || name.equals("")){
        	return 0;
        }
        int value = 0;
        if (!containsKey(name)){
            return 0;
        }
        Object obj = this.get(name);
        if (obj == null){
            return 0;
        }
        if (!(obj instanceof Integer)) {
            try {
                value = Integer.parseInt(obj.toString());
            }catch (Exception ex) {
                value = 0;
            }
        } else {
            value = ((Integer) obj).intValue();
//            obj = null;
        }
        return value;
    }

    public long getLong(String name) {
        if (name == null || name.equals("")){
            return 0;
        }
        long value = 0;
        if (!containsKey(name)){
            return 0;
        }
        Object obj = this.get(name);
        if (obj == null){
            return 0;
        }
        if (!(obj instanceof Long)) {
            try {
                value = Long.parseLong(obj.toString());
            }catch (Exception ex) {
                value = 0;
            }
        } else {
            value = ((Long) obj).longValue();
//            obj = null;
        }
        return value;
    }

    public float getFloat(String name) {
        if (name == null || name.equals("")){
            return 0;
        }
        float value = 0;
        if (!containsKey(name)){
            return 0;
        }
        Object obj = this.get(name);
        if (obj == null){
            return 0;
        }
        if (!(obj instanceof Float)) {
            try {
                value = Float.parseFloat(obj.toString());
            }catch (Exception ex) {
                value = 0;
            }
        } else {
            value = ((Float) obj).floatValue();
//            obj = null;
        }
        return value;
    }

    public double getDouble(String name) {
        if (name == null || name.equals("")){
            return 0;
        }
        double value = 0;
        if (!containsKey(name)){
            return 0;
        }
        Object obj = this.get(name);
        if (obj == null){
            return 0;
        }
        if (!(obj instanceof Double)) {
            try {
                value = Double.parseDouble(obj.toString());
            }catch (Exception ex) {
                value = 0;
            }
        } else {
            value = ((Double) obj).doubleValue();
//            obj = null;
        }
        return value;
    }

    public boolean getBoolean(String name) {
        if (name == null || name.equals("")){
            return false;
        }
        boolean value = false;
        if (!containsKey(name)){
            return false;
        }
        Object obj = this.get(name);
        if (obj == null){
            return false;
        }
        if (obj instanceof Boolean) {
            return ((Boolean) obj).booleanValue();
        }
        value = Boolean.parseBoolean(obj.toString());
//        obj = null;
        return value;
    }

    public Object getObject(String name) {
        if (name == null || name.equals("")){
            return null;
        }
        //如果该键不存在，则返回
        if (!containsKey(name)){
            return null;
        }
        return this.get(name);
    }

}
